//关键词，状态查找
function selectByStatusAndWords(productStatus, words, page, pageSize,callback) {
    $.ajax({
        type: 'post',
        url: '/product/selectByStatusAndWords',
        data: {
            productStatus: productStatus,
            words: words,
            page: page,
            pageSize: pageSize
        }
        ,success: function(result) {
            if(result.code === "SUCCESS") {
                callback(result.data);
            }else{
                alert(result.errMsg);
            }
        }
        ,error: function(error) {
            if (error != null && error.status === 401) {

            } else {
                alert("内部错误");
            }
        }
    });
}


//查询指定状态,关键词的商品页数
function selectPageCountOfStatusAndWords(productStatus, words, pageSize) {
    return new Promise((resolve, reject) => {
        $.ajax({
            type: 'post',
            url: '/product/selectPageCountOfStatusAndWords',
            data: {
                productStatus: productStatus,
                words: words,
                pageSize: pageSize
            },
            success: function(result) {
                if(result.code === "SUCCESS") {
                    resolve(result.data); //
                } else {
                    alert(result.errMsg);
                    reject(new Error(result.errMsg));
                }
            },
            error: function(error) {
                if (error.status === 401) {
                    window.location.href = '/login';
                    reject(new Error('未授权'));
                } else {
                    alert("内部错误");
                    reject(new Error("内部错误"));
                }
            }
        });
    });
}


//查询指定状态的商品
function selectProductByStatus(productStatus, page, pageSize,callback) {
    $.ajax({
        type: 'post',
        url: '/product/selectByStatus',
        data: {
            productStatus: productStatus,
            page: page,
            pageSize: pageSize
        }
        ,success: function(result) {
            if(result.code === "SUCCESS") {
               callback(result.data);
            }else{
                alert(result.errMsg);
            }
        }
        ,error: function(error) {
            if (error != null && error.status === 401) {

            } else {
                alert("内部错误");
            }
        }
    });
}

//查询指定状态的商品页数
function selectPageCountOfStatus(productStatus, pageSize) {
    return new Promise((resolve, reject) => {
        $.ajax({
            type: 'post',
            url: '/product/selectPageCountOfStatus',
            data: {
                productStatus: productStatus,
                pageSize: pageSize
            },
            success: function(result) {
                if(result.code === "SUCCESS") {
                    resolve(result.data); //
                } else {
                    alert(result.errMsg);
                    reject(new Error(result.errMsg));
                }
            },
            error: function(error) {
                if (error.status === 401) {
                    window.location.href = '/login';
                    reject(new Error('未授权'));
                } else {
                    alert("内部错误");
                    reject(new Error("内部错误"));
                }
            }
        });
    });
}


//修改指定商品状态
function updateStatusById(product_id, status) {
    $.ajax({
        type: 'post',
        url: '/product/updateById',
        data: {
            productId: product_id,
            productStatus: status
        }
        ,success: function(result) {
            if(result.code === "SUCCESS") {
                location.reload();
            }else{
                alert(result.errMsg);
            }
        }
        ,error: function(error) {
            if (error != null && error.status === 401) {
                alert("请先登录");
                location.href = "/login.html";
            } else {
                alert("内部错误");
            }
        }
    });
}


//删除指定商品
function dele(product_id) {
    $.ajax({
        type: 'post',
        url: '/product/deleteById',
        data: {
            productId: product_id
        }
        ,success: function(result) {
            if(result.code === "SUCCESS") {
                location.reload();
            }else{
                alert(result.errMsg);
            }
        }
        ,error: function(error) {
            if (error != null && error.status === 401) {
                alert("请先登录");
                location.href = "/login.html";
            } else {
                alert("内部错误");
            }
        }
    });
}
